#ifndef __AD7124_H
#define __AD7124_H


#include "main.h"



typedef struct
{
	float adc_data[8];
	float temp[8];
}TEMPERATURE;
extern TEMPERATURE temperature;




#define AD7124_SPI_HANDLE hspi1
extern SPI_HandleTypeDef AD7124_SPI_HANDLE;


#define AD7124_CS_PORT GPIOG
#define AD7124_CS_PIN  GPIO_PIN_11

//MISO与DRDY为同一引脚
#define AD7124_DRDY_PORT GPIOG
#define AD7124_DRDY_PIN  GPIO_PIN_9


/* AD7124 Register Map */
typedef enum
{
	AD7124_COMM_STATUS_REG=0,//写模式为COMM通信寄存器,读模式为STATUS状态寄存器
	AD7124_ADC_CTRL_REG,
	AD7124_DATA_REG,
	AD7124_IO_CTRL1_REG,
	AD7124_IO_CTRL2_REG,
	AD7124_ID_REG,
	AD7124_ERR_REG,
	AD7124_ERREN_REG,
	AD7124_MCLK_CNT_REG,
	AD7124_CH0_MAP_REG,
	AD7124_CH1_MAP_REG,
	AD7124_CH2_MAP_REG,
	AD7124_CH3_MAP_REG,
	AD7124_CH4_MAP_REG,
	AD7124_CH5_MAP_REG,
	AD7124_CH6_MAP_REG,
	AD7124_CH7_MAP_REG,
	AD7124_CH8_MAP_REG,
	AD7124_CH9_MAP_REG,
	AD7124_CH10_MAP_REG,
	AD7124_CH11_MAP_REG,
	AD7124_CH12_MAP_REG,
	AD7124_CH13_MAP_REG,
	AD7124_CH14_MAP_REG,
	AD7124_CH15_MAP_REG,
	AD7124_CFG0_REG,
	AD7124_CFG1_REG,
	AD7124_CFG2_REG,
	AD7124_CFG3_REG,
	AD7124_CFG4_REG,
	AD7124_CFG5_REG,
	AD7124_CFG6_REG,
	AD7124_CFG7_REG,
	AD7124_FILT0_REG,
	AD7124_FILT1_REG,
	AD7124_FILT2_REG,
	AD7124_FILT3_REG,
	AD7124_FILT4_REG,
	AD7124_FILT5_REG,
	AD7124_FILT6_REG,
	AD7124_FILT7_REG,
	AD7124_OFFS0_REG,
	AD7124_OFFS1_REG,
	AD7124_OFFS2_REG,
	AD7124_OFFS3_REG,
	AD7124_OFFS4_REG,
	AD7124_OFFS5_REG,
	AD7124_OFFS6_REG,
	AD7124_OFFS7_REG,
	AD7124_GAIN0_REG,
	AD7124_GAIN1_REG,
	AD7124_GAIN2_REG,
	AD7124_GAIN3_REG,
	AD7124_GAIN4_REG,
	AD7124_GAIN5_REG,
	AD7124_GAIN6_REG,
	AD7124_GAIN7_REG,
}AD7124_REG;


/* Communication Register bits */
#define AD7124_COMM_REG_WEN   (0 << 7)
#define AD7124_COMM_REG_WR    (0 << 6)
#define AD7124_COMM_REG_RD    (1 << 6)
#define AD7124_COMM_REG_RA(x) ((x) & 0x3F)


#define AD7124_AIN0 		0x00
#define AD7124_AIN1 		0x01
#define AD7124_AIN2 		0x02
#define AD7124_AIN3 		0x03
#define AD7124_AIN4 		0x04
#define AD7124_AIN5 		0x05
#define AD7124_AIN6 		0x06
#define AD7124_AIN7 		0x07
#define AD7124_AIN8 		0x08
#define AD7124_AIN9 		0x09
#define AD7124_AIN10 		0x0A
#define AD7124_AIN11 		0x0B
#define AD7124_AIN12 		0x0C
#define AD7124_AIN13 		0x0D
#define AD7124_AIN14 		0x0E
#define AD7124_AIN15 		0x0F
#define AD7124_INNER_TEMP 	0x10
#define AD7124_AVSS 		0x11
#define AD7124_INNER_REF 	0x12
#define AD7124_DGND		 	0x13
#define AD7124_AVDD_BY6_P	0x14
#define AD7124_AVDD_BY6_N	0x15
#define AD7124_IOVDD_BY6_P	0x16
#define AD7124_IODD_BY6_N	0x17
#define AD7124_ALDO_BY6_P	0x18
#define AD7124_ALDO_BY6_N	0x19
#define AD7124_DLDO_BY6_P	0x1A
#define AD7124_DLDO_BY6_N	0x1B
#define AD7124_20mV_P		0x1C
#define AD7124_20mV_N		0x1D




#define AD7124_USE_CFG0		0x00
#define AD7124_USE_CFG1		0x01
#define AD7124_USE_CFG2		0x02
#define AD7124_USE_CFG3		0x03
#define AD7124_USE_CFG4		0x04
#define AD7124_USE_CFG5		0x05
#define AD7124_USE_CFG6		0x06
#define AD7124_USE_CFG7		0x07




typedef enum
{
	AD7124_IO_AIN2=0x01,//IO1输出口为AIN2
	AD7124_IO_AIN3=0x02,//IO2输出口为AIN3
	AD7124_IO_AIN4=0x03,//IO3输出口为AIN4
	AD7124_IO_AIN5=0x04,//IO4输出口为AIN5
	AD7124_IO_ALL=0xFF,
}AD7124_IO_CHANNEL;

typedef enum
{
	IOUT0=0,
	IOUT1=1,
}AD7124_IOUTX;


//横流输出,最大输出电压3.1V左右
typedef enum
{
	IOUT_50uA=1,
	IOUT_100uA,
	IOUT_250uA,
	IOUT_500uA,
	IOUT_750uA,
	IOUT_1mA,
}AD7124_IOUT_CURRENT;

typedef enum
{
	CHANNEL_AIN0=0,
	CHANNEL_AIN1,
	CHANNEL_AIN2,
	CHANNEL_AIN3,
	CHANNEL_AIN4,
	CHANNEL_AIN5,
	CHANNEL_AIN6,
	CHANNEL_AIN7,
	CHANNEL_AIN8,
	CHANNEL_AIN9,
	CHANNEL_AIN10,
	CHANNEL_AIN11,
	CHANNEL_AIN12,
	CHANNEL_AIN13,
	CHANNEL_AIN14,
	CHANNEL_AIN15,
}AD7124_AIN_CHANNEL;

typedef struct
{
	uint8_t gpio_ctrl1:1;
	uint8_t gpio_ctrl2:1;
	uint8_t gpio_ctrl3:1;
	uint8_t gpio_ctrl4:1;
	uint8_t gpio_data1:1;
	uint8_t gpio_data2:1;
	uint8_t gpio_data3:1;
	uint8_t gpio_data4:1;

	uint8_t iout0:3;
	uint8_t iout1:3;
	uint8_t reserve:1;
	uint8_t PDSW:1;

	uint8_t iout0_ch:4;
	uint8_t iout1_ch:4;
}IO_CONTROL_REG1;

typedef union
{
	uint32_t value;
	uint8_t data[4];
	IO_CONTROL_REG1 reg;
}AD7124_IO_CONTROL;






void AD7124_Reset(void);
HAL_StatusTypeDef AD7124_Read_Register(uint8_t addr, uint8_t *data, uint16_t data_size);
HAL_StatusTypeDef AD7124_Write_Register(uint8_t addr, uint8_t *data, uint16_t data_size);
void Ad7124_Start_Single_Convert();
void Ad7124_Start_Continuous_Convert();
void Ad7124_Init_Signle_Mode(void);
void Ad7124_Init_Continuous(void);
uint8_t Ad7124_Read_Signle_Mode(float *data,uint8_t num);
uint8_t Ad7124_Read_Continuous_Mode(float *data,uint8_t channel);
void AD7124_IO_Output_Enable(AD7124_IO_CHANNEL io_channel);
void AD7124_IO_Output_Disable(AD7124_IO_CHANNEL io_channel);
void AD7124_SetIO_State(AD7124_IO_CHANNEL io_channel,uint8_t state);
void AD7124_IOUT_Enable(AD7124_IOUTX ioutx,AD7124_IOUT_CURRENT current,AD7124_AIN_CHANNEL iout_channel);
void AD7124_IOUT_Disable(AD7124_IOUTX ioutx);
void AD7124_VBIAS_Enable(AD7124_AIN_CHANNEL iout_channel);
void AD7124_VBIAS_Disable(AD7124_AIN_CHANNEL iout_channel);
void ADC_Data_Convert();




#endif
